/**
 * Created by huangying on 17/11/10.
 */

//后台接口url
var url_info = "";
if (document.domain == "localhost") {
    //本地
    url_info = "http://wx.mike-mary.cn/mmDirectorSystem/";
    // url_info = "http://ms.mike-mary.cn/mmDirectorSystem/";
    //url_info = "http://192.168.1.109:8080/mmDirectorSystem/";
} else if (document.domain == "wx.mike-mary.cn") {
    //测试
    //url_info = "http://120.77.244.199/mmDirectorSystem/";//后台接口url
    url_info = "http://wx.mike-mary.cn/mmDirectorSystem/";//后台接口url
} else if (document.domain == "pre.mike-mary.cn") {
    //预生产
    url_info = "http://pre.mike-mary.cn:8001/mmDirectorSystem/";//后台接口url
} else if (document.domain == "ms.mike-mary.cn") {
    //生产
    url_info = "http://ms.mike-mary.cn/mmDirectorSystem/";//后台接口url
} else {
    //hy
    url_info = "http://139.224.237.23/mmDirectorSystem/";//后台接口url
}


//ajax封装 get方法
function ppt_get_send(url_content, success_method) {
    $.ajax({
        type: "GET",
        url: url_info + url_content,
        headers: {Mauth: _uid + "_" + _token},
        async: false,
        success: function (data) {
            try {
                if (data.code == "1000") {
                    success_method(data);
                } else {
                    //if(is_not_empy(data.msg)){
                    //    layer.msg(data.msg,{time:2000});
                    //}else{
                    //    layer.msg("异常！",{time:2000});
                    //}
                }
            } catch (e) {

            }
        },
        error: function (data) {
            if (data.status == 401) {
                alert("token值错误！")
            } else if (data.status == 403) {
                if (data.responseJSON.code == 1004) {
                    alert("参数不足");
                } else {
                    alert("服务器跑火星啦~请稍后再试~");
                }

            } else {
                alert("服务器跑火星啦~请稍后再试~");
            }
        }
    });
}

// 获取url地址栏的参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


var _token = GetQueryString("token");//token
var _uid = GetQueryString("uid");//uid
var bookID = GetQueryString("bookID");//书籍的ID编号
var bookNum = GetQueryString("bookNum");//书籍麦粒码
console.log("_token:" + _token);
console.log("_uid:" + _uid);
console.log("bookID:" + bookID);
console.log("bookNum:" + bookNum);

// 配置文件内的参数
var bookOrderType = "";//获取图书翻页状态
var sum = 0;//获取图书总数
var backCoverLinkCover = "";//判断是否需要将封面图再次展示
var backCoverImg="";// 封面封底需要同时展示,需要存储第一张封面图
var specialPages = "";//判断是否存在合页内容
var j = 0;// 合页数组标号

// 爸爸带我看宇宙
// var bookID = "8a9b0aa15f6d04cc015f76ed12430017";//书籍的ID编号
// var bookNum = "9787532483235";//书籍麦粒码
// bookConfig:{
//     backCoverLinkCover:false,
//     orderType:"从左至右"
//     specialPages:[]
//     sumOfPage:32
// }

// 卡卡的魔法积木（含合页）
// var bookID = "8a9b0ac458e20f5a0158e82c833f021b";//书籍的ID编号
// var bookNum = "9787115302601";//书籍麦粒码
// bookConfig:{
//     backCoverLinkCover:false,
//     orderType:"从左至右"
//     specialPages:[34]
//     sumOfPage:44
// }

// 11只猫进袋子（封底封面同时显示）
// var bookID = "8a9b0ac458e20f5a0158e82c7ab00111";//书籍的ID编号
// var bookNum = "978780225826601";//书籍麦粒码
// bookConfig:{
//     backCoverLinkCover:true,
//     orderType:"从左至右"
//     specialPages:[]
//     sumOfPage:48
// }


// 分针大冒险（从右到左）
// var bookID = "8a9b0ac458e20f5a0158e82c7fd601b3";//书籍的ID编号
// var bookNum = "9787556014699";//书籍麦粒码
// bookConfig:{
//     backCoverLinkCover:false,
//     orderType:"从右至左"
//     specialPages:[]
//     sumOfPage:45
// }


$('#fb7-ajax').data('config', {
    "page_width": 1280,
    "page_height": 1840,
    "go_to_page": "Page",
    "gotopage_width": "45",
    "zoom_double_click": "1",
    "zoom_step": "0.06",
    "tooltip_visible": "true",
    "toolbar_visible": "true",
    "deeplinking_enabled": "true",
    "double_click_enabled": "true",
    "rtl": "false"
});

//获取ppt的基本信息
bookNumInfo();

//获取ppt的基本信息
function bookNumInfo() {
    var bookNum = 0;//书本麦粒码
    ppt_get_send("activity/getBookMedia?bookID=" + bookID, function (data) {
        //存在PPT
        var _bookInfo = data.object.bookConfig
        if (_bookInfo) {
            //存在配置文件
            sum = _bookInfo.sumOfPage;//总页数
            bookOrderType = _bookInfo.orderType;
            specialPages = _bookInfo.specialPages;//判断是否存在合页内容
            backCoverLinkCover = _bookInfo.backCoverLinkCover;//判断是否需要将封面图再次展示

            for (var i = 1; i <= sum; i = i + 10) {
                getImageURL(i, true);
            }
        } else {
            getImageURL(bookNum, 1, 1, false);
        }
    });
}

//有config配置文件时，获取页面信息
function getImageURL(p, state) {
    //p:获取图片url的页码
    //state:true:存在config文件; false:不存在

    var deepLinkingInfo = "";//书籍列表展示
    var bookInfo = "";//具体书籍内容
    var menuInfo = "";//目录页面
    ppt_get_send("activity/getBookPages?bookNum=" + bookNum + "&page=" + p, function (data) {
        if (state == true) {
            //存在config文件
            $.each(data.object, function (i, item) {
                var _newPageClass = parseInt(p) + i + j;
                if (backCoverLinkCover == true && _newPageClass == 1) {
                    // 封面封底需要同时展示,需要存储第一张封面图
                    backCoverImg = item;
                }
                if (_newPageClass < sum) {
                    // 书籍列表展示
                    if (_newPageClass == specialPages[j]) {
                        // 合页
                        deepLinkingInfo = '<li data-address="page' + _newPageClass + '-' + parseInt(_newPageClass + 1) + '" data-page="' + _newPageClass + '"></li>' +
                            '<li data-address="page' + _newPageClass + '-' + parseInt(_newPageClass + 1) + '" data-page="' + parseInt(_newPageClass + 1) + '"></li>';
                    } else {
                        deepLinkingInfo = '<li data-address="page' + _newPageClass + '" data-page="' + _newPageClass + '"></li>';
                    }
                    $('#fb7-deeplinking ul').append(deepLinkingInfo);


                    // 具体书籍内容
                    if (_newPageClass == specialPages[j]) {
                        // 合页
                        bookInfo += '<div style="background-image:url(' + item + ')" class="fb7-double fb7-first fb7-noshadow">' +
                            '<div class="fb7-cont-page-book">' +
                            '<div class="fb7-page-book"></div>' +
                            '</div>' +
                            '</div>' +
                            '<div style="background-image:url(' + item + ')" class="fb7-double fb7-second fb7-noshadow">' +
                            '<div class="fb7-cont-page-book">' +
                            '<div class="fb7-page-book"></div>' +
                            '</div>' +
                            '</div>';
                    } else {
                        if (p == 1 && i == 0) {
                            bookInfo += '<div style="background-image:url(' + item + ')" class="fb7-noshadow">'
                        } else {
                            bookInfo += '<div style="background-image:url(' + item + ')">'
                        }
                        bookInfo += '<div class="fb7-cont-page-book">' +
                            '<div class="fb7-page-book"></div>' +
                            '</div>' +
                            '</div>';
                    }

                    $('#fb7-book').append(bookInfo);


                    // 目录页面
                    if (_newPageClass == specialPages[j]) {
                        // 合页
                        menuInfo = '<li class="' + parseInt(_newPageClass + 1) + '"><img alt="" src="' + item + '"></li>';

                    } else {
                        menuInfo = '<li class="' + _newPageClass + '"><img alt="" src="' + item + '"></li>';
                    }

                    $('#fb7-slider').append(menuInfo);

                } else if (_newPageClass == sum) {
                    // 最后一页
                    // 书籍列表展示
                    deepLinkingInfo += '<li data-address="page' + _newPageClass + '" data-page="' + _newPageClass + '"></li>';
                    if (backCoverLinkCover == true) {
                        //封面封底需要同时展示
                        deepLinkingInfo += '<li data-address="page' + (_newPageClass + 1) + '" data-page="' + (_newPageClass + 1) + '"></li>';
                    }
                    $('#fb7-deeplinking ul').append(deepLinkingInfo);


                    // 具体书籍内容
                    bookInfo += '<div style="background-image:url(' + item + ')">' +
                        '<div class="fb7-cont-page-book">' +
                        '<div class="fb7-page-book"></div>' +
                        '</div>' +
                        '</div>';
                    if (backCoverLinkCover == true) {
                        //封面封底需要同时展示
                        bookInfo += '<div style="background-image:url(' + backCoverImg + ')">' +
                            '<div class="fb7-cont-page-book">' +
                            '<div class="fb7-page-book"></div>' +
                            '</div>' +
                            '</div>';
                    }
                    $('#fb7-book').append(bookInfo);

                    // 目录页面
                    menuInfo += '<li class="' + _newPageClass + '"><img alt="" src="' + item + '"></li>';
                    if (backCoverLinkCover == true) {
                        //封面封底需要同时展示
                        menuInfo += '<li class="' + _newPageClass + '"><img alt="" src="' + backCoverImg + '"></li>';
                    }
                    $('#fb7-slider').append(menuInfo);
                }
                // 参数内容置空
                deepLinkingInfo = "";//书籍列表展示
                bookInfo = "";//具体书籍内容
                menuInfo = "";//目录页面
            })
        } else {
            //不存在
            pingTest(data.object, p)
        }
    });
}

//没有config配置文件时,判断url是否有效
function pingTest(img_url, p) {
    var deepLinkingInfo = "";//书籍列表展示
    var bookInfo = "";//具体书籍内容
    var menuInfo = "";//目录页面

    $.ajax({
        type: "GET",
        url: img_url,
        async: false,
        success: function () {
            deepLinkingInfo = '<li data-address="page' + p + '" data-page="' + p + '"></li>';
            $('#fb7-deeplinking ul').append(deepLinkingInfo);
            if (p == 1) {
                bookInfo += '<div style="background-image:url(' + img_url + ')" class="fb7-noshadow">'
            } else {
                bookInfo += '<div style="background-image:url(' + img_url + ')">'
            }
            bookInfo += '<div class="fb7-cont-page-book">' +
                '<div class="fb7-page-book"></div>' +
                '</div>' +
                '</div>';
            $('#fb7-book').append(bookInfo);
            menuInfo = '<li class="' + p + '"><img alt="" src="' + img_url + '"></li>';
            $('#fb7-slider').append(menuInfo);
            getImageURL(p + 1, p + 1, false);
        },
        error: function () {

        }
    });
}

window.onload=function () {
    if (bookOrderType == "从右至左") {
        console.log(sum)
        console.log(specialPages.length)
        console.log(sum + specialPages.length)
        setPage(sum + specialPages.length);
    }
}
